package com.rongzhe.house.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.rongzhe.house.R;
import com.rongzhe.house.entity.voo.HouseDetail;
import com.rongzhe.house.entity.voo.User;
import com.rongzhe.house.manager.OrderManager;
import com.rongzhe.house.presenter.OrderPresenter;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by huang on 2018/1/4.
 */

public class OrderActivity extends BaseActivity<OrderPresenter> {
    @BindView(R.id.image_house_photo)
    ImageView imageHousePhoto;
    @BindView(R.id.text_house_name)
    TextView textHouseName;
    @BindView(R.id.text_price)
    TextView textPrice;
    @BindView(R.id.text_house_area)
    TextView textHouseArea;
    @BindView(R.id.text_price_a_month)
    TextView textPriceAMonth;
    @BindView(R.id.text_all_rent_price)
    TextView textAllRentPrice;
    @BindView(R.id.text_deposit)
    TextView textDeposit;
    @BindView(R.id.text_all_price)
    TextView textAllPrice;
    @BindView(R.id.text_user_name)
    TextView textUserName;
    @BindView(R.id.text_idcard_number)
    TextView textIdcardNumber;
    @BindView(R.id.text_rent_time)
    TextView textRentTime;
    @BindView(R.id.text_pay_type)
    TextView textPayType;
    @BindView(R.id.text_house_info)
    TextView textHouseInfo;

    @Override
    protected OrderPresenter createPresenter() {
        return new OrderPresenter(this);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        ButterKnife.bind(this);
    }

    public void setHouseInfo(HouseDetail houseDetail) {
        if (houseDetail.getHouseImageList() != null && houseDetail.getHouseImageList().size() > 0
                && !TextUtils.isEmpty(houseDetail.getHouseImageList().get(0).getUrl())) {
            Picasso.with(this)
                    .load(houseDetail.getHouseImageList().get(0).getUrl()).into(imageHousePhoto);
        }
        textHouseName.setText(houseDetail.getName());
        textPrice.setText(houseDetail.getRent().toString() + "元/月");
        textHouseArea.setText(houseDetail.getArea().toString() + "m²");
        String format = getString(R.string.format_price);
        textPriceAMonth.setText("租金" + houseDetail.getRent().toString() + "元/月");
        textAllRentPrice.setText(String.format(format,
                Float.parseFloat(houseDetail.getRent().toString()) * 3 + ""));
        textHouseInfo.setText(houseDetail.getType() + "-" + (houseDetail.getArea() + "㎡"));
        textDeposit.setText(String.format(format, houseDetail.getRent().toString()));
        String payType = OrderManager.getInstance().getPayTypeId();
        int amount = 4;
        if (!TextUtils.isEmpty(payType)) {
            switch (payType) {
                //押一付一
                case "1":
                    amount = 2;
                    break;
                //押一付三
                case "2":
                    amount = 4;
                    break;
                //押一付六
                case "3":
                    amount = 7;
                    break;
            }
        }
        textAllPrice.setText(String.format(format,
                Float.parseFloat(houseDetail.getRent().toString()) * amount + ""));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar endTime = Calendar.getInstance();
        endTime.add(Calendar.MONTH, 6);
        textRentTime.setText(simpleDateFormat.format(Calendar.getInstance().getTime()) + " 至 "
                + simpleDateFormat.format(endTime.getTime()));

    }

    public void setUserInfo(User user) {
        //TODO real user info
        textUserName.setText("老王");
        textIdcardNumber.setText("500382199004060876");
    }

    public void setPayType(String payType) {
        if (!TextUtils.isEmpty(payType)) {
            textPayType.setText(String.format(getString(R.string.pay_type_format), payType));
        }
    }

    @OnClick(R.id.btn_submit)
    public void onSubmitClick() {
        mPresenter.doSubmit();
    }

    @Override
    protected String getActionTitle() {
        return getString(R.string.order_info);
    }
}
